package com.cefa.finance.controller;

import com.cefa.finance.model.Account;
import com.cefa.finance.model.Contract;
import com.cefa.finance.result.ResultModel;
import com.cefa.finance.service.UserAccountInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("userAccountInfo")
public class UserAccountInfoController {

    private static final Logger logger = LoggerFactory.getLogger(UserAccountInfoController.class);

    @Autowired
    private UserAccountInfoService userAccountInfoService;


    @RequestMapping(path = "findNameByPhone", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel findNameByPhone(Account source) {
        try {
            return userAccountInfoService.findNameByPhone(source);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }


    @RequestMapping(path = "openAccount", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel openAccount(Account source) {
        try {
            return userAccountInfoService.openAccount(source);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }


    @RequestMapping(path = "listUserAccounts", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel listUserAccounts(Contract contract) {
        try {
            return userAccountInfoService.listUserAccounts(contract);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "userLoginIndex", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel userLoginIndex(Contract contract) {
        try {
            return userAccountInfoService.userLoginIndex(contract);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "downLoadPrivatekey", method = RequestMethod.GET)
    @CrossOrigin
    public ResultModel downLoadPrivatekey(HttpServletResponse response, Long loginUserId) {
        try {
            return userAccountInfoService.downLoadPrivatekey(response,loginUserId);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }
}
